<?php
/**
 * @title 373. 查找和最小的 K 对数字
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

/**
 * Class iMinHeap 最小堆
 */
class MyMinHeap extends SplMinHeap{
    public function compare($arr1, $arr2)
    {
        if($arr1[0] <= $arr2[0]) {
            return 1;
        } else {
            return -1;
        }
    }
}

class Solution {

    /**
     * @param Integer[] $nums1
     * @param Integer[] $nums2
     * @param Integer $k
     * @return Integer[][]
     */
    function kSmallestPairs($nums1, $nums2, $k) {
        /**
         * @since 2023-12-23 数组元素数量
         */
        $nums1Count = count($nums1);
        $nums2Count = count($nums2);
        
        /**
         * @since 2023-12-23 新建一个堆
         */
        $heap = new MyMinHeap();

        /**
         * @since 2023-12-23 $nums1和$nums2的第一个元素入堆
         */
        $heap->insert([$nums1[0] + $nums2[0], 0, 0]);

        /**
         * @since 2023-12-23 防止元素重复入堆
         */
        $hashArray = [];
        $hashArray[0][0] = true;

        /**
         * @since 2023-12-23 选出k个最小的和
         */
        $result = [];
        for($i=0; $i<$k; $i++){
            /**
             * @since 2023-12-23 堆为空，跳出循环
             */
            if($heap->count() == 0){
                break;
            } else {}

            /**
             * @since 2023-12-23 弹出堆顶元素
             */
            $top = $heap->extract();

            /**
             * @since 2023-12-23 $nums1和$nums2的指针
             */
            $nums1Pointer = $top[1];
            $nums2Pointer = $top[2];
            // echo "nums1Pointer: {$nums1Pointer}, nums2Pointer: {$nums2Pointer}, top: {$top[0]}", PHP_EOL;

            /**
             * @since 2023-12-23 记录对值
             */
            $result[] = [$nums1[$nums1Pointer], $nums2[$nums2Pointer]];

            /**
             * @since 2023-12-23 新的数据压入栈
             */
            if($nums1Pointer <= $nums1Count - 2 AND !isset($hashArray[$nums1Pointer + 1][$nums2Pointer])) {
                $heap->insert([$nums1[$nums1Pointer + 1] + $nums2[$nums2Pointer], $nums1Pointer + 1, $nums2Pointer]);
                $hashArray[$nums1Pointer + 1][$nums2Pointer] = true;
                // echo "Insert Heap，nums1Pointer: ", ($nums1Pointer+1), ", nums2Pointer: {$nums2Pointer}", PHP_EOL;
            } else {}
            if($nums2Pointer <= $nums2Count - 2 AND !isset($hashArray[$nums1Pointer][$nums2Pointer + 1])) {
                $heap->insert([$nums1[$nums1Pointer] + $nums2[$nums2Pointer + 1], $nums1Pointer, $nums2Pointer + 1]);
                $hashArray[$nums1Pointer][$nums2Pointer + 1] = true;
                // echo "Insert Heap，nums1Pointer: {$nums1Pointer}, nums2Pointer: ", ($nums2Pointer+1), PHP_EOL;
            } else {}
            // echo str_repeat(PHP_EOL, 2);
        }

        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202312/2023.12.23_2.php
 */
$datas = [
    [[1,7,11], [2,4,6], 3],
    [[1,1,2], [1,2,3], 2],
    [[1,2], [3], 3 ],
    [[1,2,4,5,6], [3,5,7,9], 20], // [[1,3],[2,3],[1,5],[2,5],[4,3],[1,7],[5,3],[2,7],[4,5],[6,3],[1,9],[5,5],[2,9],[4,7],[6,5],[5,7],[4,9],[6,7],[5,9],[6,9]]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->kSmallestPairs(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
